package mathFrame

import scalafx.scene.layout.StackPane
import scalafx.scene.layout.Region
import scalafx.scene.shape._

class Plotter2D(prefH: Double, prefW: Double) extends StackPane {
  prefHeight = prefH
  prefWidth = prefW
  maxHeight = Region.USE_PREF_SIZE
  maxWidth = Region.USE_PREF_SIZE
//  maxSize(Region.USE_PREF_SIZE, Region.USE_PREF_SIZE)
  
  var function: UserFunction1P = NoValue
  var minX = -3.14
  var maxX = 3.14
  var step = 0.01
  var escalaX = 100
  var escalaY = -100
  var transposeX = 3.14
  var transposeY = -3.14
  
  private def calculateX(x: Double) = (x + transposeX) * escalaX
  private def calculateY(y: Double) = (y + transposeY) * escalaY
  
  def regenerate() {
    this.getChildren.clear
    if (function != NoValue) {
      var vx = minX
      val path = new Path
      path.getElements add MoveTo(calculateX(vx), calculateY(function(vx)))
      while (vx < maxX) {
        vx += step
        path.getElements add LineTo(calculateX(vx), calculateY(function(vx)))
      }
      this.getChildren add path
    }
  }
}
